package pro.softzhang.algo.lc1700;

import pro.softzhang.algo.flag.book.BDaily;
import pro.softzhang.algo.flag.knowledge.KString;

/**
 *
 */
@BDaily
@KString
public class LC1790_CheckIfOneStringSwapCanMakeStringsEqual {
    public static void main(String[] args) {
        String s1 = "aa", s2 = "ac";
        System.out.println(new Solution().areAlmostEqual(s1, s2));
    }

    /**
     *
     */
    static
    class Solution {
        public boolean areAlmostEqual(String s1, String s2) {
            int adi = -1, bdi = -1;
            for (int i = 0; i < s1.length(); i++) {
                if (s1.charAt(i) == s2.charAt(i))
                    continue;
                if (adi == -1)
                    adi = i;
                else if (bdi == -1)
                    bdi = i;
                else
                    return false;
            }

            if (adi == -1 && bdi == -1)
                return true;

            if (adi == -1 || bdi == -1)
                return false;

            return s1.charAt(adi) == s2.charAt(bdi) && s1.charAt(bdi) == s2.charAt(adi);
        }
    }
}
